package test.book;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class RecordCrudSample {
    
    public static void main(String[] args) {
     // 创建name属性为James,age属性为25的record对象并添加到数据库
        Record user = new Record().set("name", "James").set("age", 25);
        Db.save("user", user);
         
        // 删除id值为25的user表中的记录
        Db.deleteById("user", 25);
         
        // 查询id值为25的Record将其name属性改为James并更新到数据库
        user = Db.findById("user", 25).set("name", "James");
        Db.update("user", user);
         
        // 查询id值为25的user, 且仅仅取name与age两个字段的值
        user = Db.findById("user", "25", "name, age");
         
        // 获取user的name属性
        String userName = user.getStr("name");
         
        // 获取user的age属性
        Integer userAge = user.getInt("age");
         
        // 查询所有年龄大于18岁的user,并输出其name属性
        List<Record> users = Db.find("select * from user where age > 18");
         
        // 分页查询性别为1并且年龄大于18的user,当前页号为1,每页10个user
        Page<Record> userPage = Db.paginate(1, 10, "select *", "from user where sex=? and age>?", 1, 18);
    }

}
